# encoding: utf-8
__author__ = 'wwj'
import re
import sys
reload(sys)
sys.setdefaultencoding('utf8')
sys.path.append("/Users/wwj/Downloads/shangtianjun/girl/")
from girl.dbShell.dbshell import dbshell
# from ..dbshell import dbshell

class main(dbshell):
    orderItem = [
            ['url', ''],
            ['name', ''],
            ['workTime', ''],
            ['email', ''],
            ['address', ''],
            ['phone', ''],
            ['site', ''],
            ['goto', ''],
            ['info', ''],
            ['mode', ''],
            ['cate', ''],
            ['price', ''],
            ['prix', ''],
            ['modeId', ''],
            ['cateId', ''],
            ['area', ''],
            ['city', ''],
            ['town', ''],
            ['cover', ''],
    ]
    existgirl = {}
    mongoTable = "store"
    mysqltable = "store"
    emails = {}
    reg = re.compile(r"\s(\S+)\((\S+)/")
    where = {}
    def __init__(self,spidername):
        super(main,self).__init__(spidername)
        self.geturltool("select distinct(url) from store")
        self.getdata()


    def getdata(self):
        sql = "select name from mode union select name from cate"
        cateLsit = self.getdb(sql)
        self.cateLsit = "|".join([x.strip() for x in cateLsit])
        tmp = self.getdb("select id,name from cate")
        self.cate = [{"id":x.split()[0],"name":x.split()[1]} for x in tmp]
        tmp = self.getdb("select id,name from mode")
        self.mode = [{"id":x.split()[0],"name":x.split()[1]} for x in tmp]
        tmp = self.getdb("select id,name,level from area")
        self.area = [{"id":x.split()[0],"name":x.split()[1],"level":x.split()[2]} for x in tmp]
        for y in self.client[self.config.get('mongoDb')]["email"].find():
            self.emails[y["storesUrl"]] = y["email"]

    def clean(self,y):
        data = self.data
        data["url"] = y["url"].replace("https://www.cityheaven.net","")
        data["workTime"] = y["workTime"][1] if type(y["workTime"]) == list else y["workTime"]
        data["name"] = y["name"]
        data["cover"] = ",".join(y["cover"])
        data["email"] = self.emails.get(y["url"],"")
        data["address"] = re.sub("\[.+\]","",y["address"] or "")
        xx = "派遣エリア：".decode("utf")
        data["address"] = data["address"].replace(xx,"")
        data["address"] = "" if len(data["address"]) > 200 else data["address"]
        if y.get("phone"):
            data["phone"] = y.get("phone").strip()
        if y.get("site"):
            data["site"] = ",".join(y["site"]) if type(y["site"]) == list else y["site"]
        else:
            data["site"] = ""
        data["goto"] = "" if type(y.get("goto")) == list else y.get("goto","") or ""
        data["info"] = y["info"] or ""
        if not y["cate2"] and not y["cate1"]:
            data["cate"] = "dispatch"
            data["mode"] = "dispatch"
        else:
            data["cate"] = y["cate2"]
            data["mode"] = y["cate1"]
        if not y["cate1"] and y["cate2"].find("(") > 0:
            if self.reg.search(y["cate2"]):
                data["mode"] = self.reg.search(y["cate2"]).groups()[0]
                data["cate"] = self.reg.search(y["cate2"]).groups()[1]
        data["price"] = y["price"] or ""
        if y["price"]:
            try:
                prix = re.search(r'(\d+)\D+(\d+)',y['price']).groups()
                data['prix'] = str(int(prix[1])/int(prix[0]) * 60)
            except:
                data['prix'] = ""

        # data["urlHash"] = str(crc32(y["storesUrl"])).replace("-","")
        for yy in self.cate:
            if yy["name"] == data["cate"]:
                data["cateId"] = yy["id"]
        for yy in self.mode:
            if yy["name"] == data["mode"]:
                data["modeId"] = yy["id"]
        if y["area"]:
            for yy in self.area:
                if yy["name"] == re.sub(self.cateLsit,"",y["area"][1]) and int(yy["level"]) == 1:
                    data['area'] = yy["id"]
                elif yy["name"] == re.sub(self.cateLsit,"",y["area"][3]) and int(yy["level"]) == 2:
                    data['city'] = yy["id"]
                elif yy["name"] == re.sub(self.cateLsit,"",y["area"][4]) and int(yy["level"]) == 3:
                    data['town'] = yy["id"]
            # import ipdb;ipdb.set_trace()

        # urlpool.add(y["storesUrl"])


if __name__ == '__main__':
    obj = main("city")
    obj()
